package com.example.springboot.controller;

import com.example.springboot.common.Result.Result;
import com.example.springboot.common.system.UserHolder;
import com.example.springboot.common.utils.RedisUtil.RedisUtil;
import com.example.springboot.common.utils.StringUtils.StringUtils;

import com.example.springboot.entity.dto.LoginDto;
import com.example.springboot.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import static com.example.springboot.common.constants.Constants.*;
import static com.example.springboot.common.utils.UserHolder.UserHolderUtil.getToken;

@Slf4j
@RestController
@RequestMapping("/user")
public class LoginController {
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private LoginService loginService;

    /**
     * 登录
     */
    @PostMapping("/login")
    public Result<?> login(@RequestBody LoginDto loginDto) {
        return Result.success(loginService.login(loginDto));
    }

    /**
     * 查询用户信息
     */
    @GetMapping("/info")
    public Result<?> info() {
        return Result.success(loginService.info());
    }

    /**
     * 获取路由信息
     */
//    @GetMapping("/getRouters")
//    public Result<?> getRouters() {
//        Long userId = UserHolder.getLoginUser().getUser().getId();
//        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
////        return AjaxResult.success(menuService.buildMenus(menus));
//    }

    /**
     * 退出
     */
    @PostMapping("/logout")
    public Result<?> logout() {
        if (StringUtils.isNotNull(getToken())) {
            redisUtil.deleteObject(USER_REDIS + getToken());
        }
        return Result.success();
    }

    /**
     * 验证码获取接口
     */
    @GetMapping("/checkCode")
    public Result<?> checkCode() {
        return Result.success(loginService.checkCode());
    }
}
